What is lodash.flatmap?
The lodash.flatmap package is a utility library that provides a method to map and flatten a collection in a single operation. It is part of the Lodash library, which is a popular JavaScript utility library that offers a wide range of functions for common programming tasks.
Map and Flatten
This feature allows you to map each element in a collection to a new value and then flatten the result into a single array. In this example, we map each user to their pets and then flatten the resulting arrays into a single array of pet names.
const _ = require('lodash.flatmap');
const users = [
{ 'user': 'barney', 'age': 36, 'pets': ['hoppy'] },
{ 'user': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
];
const result = _.flatMap(users, function(o) { return o.pets; });
console.log(result); // ['hoppy', 'baby puss', 'dino']